package com.chima.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Post;
import com.chima.service.PostService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import flexjson.JSONSerializer;
public class PostAction extends ActionSupport implements ModelDriven<Post> {
	  Post post= new Post();
		public Post getModel() {	
			return post;
		}
		private int start;// 开始页码
		private int limit; //每页显示
		private int page;//页数
		private boolean success=false ;
		private PostService postService;
		private List<Post> posts;
		private int param;//根据条件查询科室
		private int depart;
		
		public int getDepart() {
			return depart;
		}

		public void setDepart(int depart) {
			this.depart = depart;
		}

		public int getParam() {
			return param;
		}

		public void setParam(int param) {
			this.param = param;
		}

		public PostService getPostService() {
			return postService;
		}

		public void setPostService(PostService postService) {
			this.postService = postService;
		}

		public List<Post> getPosts() {
			return posts;
		}

		public void setPosts(List<Post> posts) {
			this.posts = posts;
		}

		public boolean isSuccess() {
			return success;
		}

		public void setSuccess(boolean success) {
			this.success = success;
		}

		

		public int getStart() {
			return start;
		}

		public void setStart(int start) {
			this.start = start;
		}

		public int getLimit() {
			return limit;
		}

		public void setLimit(int limit) {
			this.limit = limit;
		}

		public int getPage() {
			return page;
		}

		public void setPage(int page) {
			this.page = page;
		}

		public String  ListPost() throws Exception{
		    PageRequestBean pageRequestBean = new PageRequestBean();
		    DetachedCriteria criteria = DetachedCriteria.forClass(Post.class);
			if(param!=0){ 
				criteria.add(Restrictions.eq("department.id",param));
			}
			if(depart!=0){ 
				criteria.add(Restrictions.eq("department.id",depart));
			}
			pageRequestBean.setCriteria(criteria);
			pageRequestBean.setStart(start);
			pageRequestBean.setLimit(limit);
			pageRequestBean.setPage(page);
			pageRequestBean.setDept(depart);
			PageResponseBean pageResponseBean = postService.findPost(pageRequestBean);
			posts=pageResponseBean.getRows();
	        int total=0;
	        total =pageResponseBean.getTotal();
			// 输出json内容到客户端
	        JSONSerializer jsonSerializer = new JSONSerializer();
	        jsonSerializer.include("*.id","*.name","*.department");
	        jsonSerializer.exclude("*.class","*.jobDetails","*.records","*.jobs","*.employees","*.weights");
	        String resultJson1 = jsonSerializer.deepSerialize(posts);  
	        String resultJson2 =jsonSerializer.serialize(total);
	        System.out.println(resultJson2);
	        String jsonString = "{total:"+resultJson2.toString()+",posts:"+resultJson1.toString()+"}";
	        System.out.print(jsonString);
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
			return NONE;
	  }
		public void addPost() throws Exception{
			try {
				postService.savePost(post);
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			this.success=true;
			JSONSerializer jsonSerializer = new JSONSerializer();
			String resultJson =jsonSerializer.serialize(success);
			String jsonString = "{success:"+resultJson.toString()+"}";
			System.out.println(jsonString);
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
		}
		
		public void editPost() throws Exception{
		postService.updatePost(post);
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	public String deletePost(){
			postService.deletePost(post.getId());
		return NONE;
	}

	public String  ListPostBydepart() throws Exception{
		   posts=postService.findPostByDept(param);
	       int  total =posts.size();
			// 输出json内容到客户端
	        JSONSerializer jsonSerializer = new JSONSerializer();
	        jsonSerializer.exclude("*.class");
	        String resultJson1 = jsonSerializer.deepSerialize(posts);  
	        String resultJson2 =jsonSerializer.serialize(total);
	        String jsonString = "{total:"+resultJson2.toString()+",posts:"+resultJson1.toString()+"}";
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
			return NONE;
	  }
}
